<?php

namespace App\V3\V3_2_7\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use App\V3\V3_2_7\Services\BuyStorageSpaceService;
use App\V3\V3_2_7\Models\BuyStorageSpaceModel;

class BuyStorageSpaceController extends BaseController
{

    public function add(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'set_space' =>'required|integer|min:0',//购买存储空间
            'price'     =>'required|numeric|min:0'//购买存储空间费用
        ], [
            'set_space.integer' => '参数`购买存储空间`缺失',
            'set_space.min'  => '参数`购买存储空间`最小只能为0',
            'price.required' => '参数`购买存储空间费用`缺失',
            'price.numeric'  => '参数`购买存储空间费用`仅能为数字',
            'price.min'  => '参数`购买存储空间费用`最小只能为0',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }
        DB::beginTransaction();
        try {
            $data['created_at']=time();
            $id = BuyStorageSpaceService::add($data);
            if($id){
                DB::commit();
                $this->log("添加存储空间选项", $data,'存储空间'.$data['set_space'].',创建成功！');
                return $this->success();
            }
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->fail('',$e->getMessage());
        }
    }

   public function del(Request $request){

       if(empty($request->ids)) return $this->fail('','参数`购买存储空间选项id`缺失');

       $idArr=explode(',', $request->ids);
       DB::beginTransaction();
       try {
        $bool = BuyStorageSpaceModel::whereIn('id',$idArr)->delete();
        if($bool){
            DB::commit();
            $this->log("删除存储空间选项", $idArr, '删除成功！');
            return $this->success();
        }
       } catch (\Exception $e) {
           DB::rollBack();
           $this->fail('',$e->getMessage());
       }

   }

   public function info(Request $request){
       $info = BuyStorageSpaceModel::find($request->id);
       $this->log("查询存储空间选项信息", $info);
       return $this->success($info);
   }
    public function edit(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id'=>'required|integer|min:0',//购买存储空间选项id
            'set_space' =>'required|integer|min:0|max:9999',//购买存储空间
            'price'     =>'required|numeric|min:0|max:99999.99'//购买存储空间费用
        ], [
            'id.integer' => '参数`购买存储空间选项id`缺失',
            'id.min'  => '参数`购买存储空间选项id`最小只能为0',
            'id.integer'  => '参数`购买存储空间选项id`仅能为数字',
            'set_space.integer' => '参数`购买存储空间`缺失',
            'set_space.min'  => '参数`购买存储空间`最小只能为0',
            'set_space.max'  => '参数`购买存储空间`最大仅能为99999',
            'price.required' => '参数`购买存储空间费用`缺失',
            'price.numeric'  => '参数`购买存储空间费用`仅能为数字',
            'price.min'  => '参数`购买存储空间费用`最小只能为0',
            'price.max'  => '参数`购买存储空间费用`最大仅能为99999.99',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }
        DB::beginTransaction();
        try {
            $data['created_at']=time();
            $id = BuyStorageSpaceService::edit($data);
            if($id){
                DB::commit();
                $this->log("修改存储空间选项", $data, '修改成功');
                return $this->success();
            }
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->fail('',$e->getMessage());
        }
    }

    public function list(Request $request){
        $list=BuyStorageSpaceService::list();
        $this->log("存储空间选项列表", $list);
        return $this->success($list);
    }
}
